package com.zw.admin.server.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.zw.admin.server.component.PurchOrderComponent;
import com.zw.admin.server.component.StockComponent;
import com.zw.admin.server.dao.PGoodsDao;
import com.zw.admin.server.dao.PPurchOrderItemsDao;
import com.zw.admin.server.dao.PPurchOrdersDao;
import com.zw.admin.server.dto.ResponseInfo;
import com.zw.admin.server.model.PGoods;
import com.zw.admin.server.model.PPurchOrderItems;
import com.zw.admin.server.model.PPurchOrders;
import com.zw.admin.server.service.PurchOrderItemsService;

/**
 * 采购商品
 * @author idmin
 *
 */
@Service
public class PurchOrderItemsServiceImpl implements PurchOrderItemsService {

	@Autowired
	private PPurchOrdersDao pPurchOrdersDao;
	
	@Autowired
	private PPurchOrderItemsDao pPurchOrderItemsDao;
	
	@Autowired
	private PGoodsDao pGoodsDao;
	
	@Autowired
	private PurchOrderComponent purchOrderComponent;
	@Autowired
	private StockComponent stockComponent; 

	private static final Logger log = LoggerFactory.getLogger("PurchOrder");

	/**
	 * 查找并添加
	 */
	@Override
	public ResponseInfo itemQueryAdd(JSONObject request) {
		if(request.getInteger("orderId")==null) {
			return new ResponseInfo("-1","采购单号必需");
		}
		if(!(StringUtils.isNotBlank(request.getString("itemNo")) || StringUtils.isNotBlank(request.getString("sku")))) {
			return new ResponseInfo("-1","商品编号 或库存编号sku必需");
		}
		
		PPurchOrders pPurchOrders=pPurchOrdersDao.getById(request.getLong("orderId"));
		if(pPurchOrders==null) {
			return new ResponseInfo("-2","采购单不存在");
		}
		if(pPurchOrders.getStatus()>1) {
			return new ResponseInfo("-3","采购单已提交供应商采购，无法再添加商品");
		}
		//商品已经存在
		Map<String, Object> params = new HashMap<>();
		params.put("orderId", request.getInteger("orderId"));
		if(StringUtils.isNotBlank(request.getString("itemNo"))) {
			params.put("itemNo", request.getString("itemNo"));
		}
		if(StringUtils.isNotBlank(request.getString("sku"))) {
			params.put("sku", request.getString("sku"));
		}
		List<PPurchOrderItems> listItems =pPurchOrderItemsDao.list(params, 0, 1);
		if(listItems!=null && !listItems.isEmpty()) {
			return new ResponseInfo("-4","采购单已存在该商品，无法再添加商品");
		}
		//商品正确
		List<PGoods>  listGoods= pGoodsDao.list(params, 0, 1);
		if(listGoods==null || listGoods.isEmpty() ) {
			return new ResponseInfo("-5","商品未找到，无法再添加商品");
		}
		PGoods rowGoods= listGoods.get(0);
		PPurchOrderItems pPurchOrderItems= new PPurchOrderItems();
		
		pPurchOrderItems.setBuydate(pPurchOrders.getBuydate());
		pPurchOrderItems.setItemNo(rowGoods.getItemNo());
		pPurchOrderItems.setSku(rowGoods.getSku());
		pPurchOrderItems.setGoodsName( rowGoods.getGoodsName());
		pPurchOrderItems.setOrderId(pPurchOrders.getId().intValue());
		pPurchOrderItems.setPreBuyPrice(rowGoods.getPreBuyCost() );
		
		pPurchOrderItems.setQty(request.getBigDecimal("qty") );
		
		pPurchOrderItemsDao.insert(pPurchOrderItems);
		
		purchOrderComponent.saveCulcalPPurchOrders(request.getLong("orderId"));
		return new ResponseInfo("0","添加商品成功");
	}
	
	/**
	 * 保存 确认提交商品
	 * @param request
	 * @return
	 */
	@Override
	public ResponseInfo saveAll(JSONObject request) {
		 
		Long orderId =  request.getLong("orderId");
		Integer type = request.getInteger("type");
		String qtysStr =request.getString("qtys");
		List<JSONObject> listQty=JSONObject.parseArray(qtysStr,JSONObject.class);
		
		if(listQty!=null && !listQty.isEmpty()) {
			for(JSONObject jo : listQty) {
				Integer data_id = jo.getInteger("data_id");
				if(data_id == null || data_id==0) {
					continue ;
				}
				BigDecimal qty = jo.getBigDecimal("value");
				//
				PPurchOrderItems pPurchOrderItems= new PPurchOrderItems();
				pPurchOrderItems.setId((long)data_id);
				pPurchOrderItems.setQty(qty);
				pPurchOrderItemsDao.update(pPurchOrderItems);
			}
		}
		
		//确认提交 
		if(type==1) {
			purchOrderComponent.updatePurchOrderStatus(orderId, 2);
			
			return new ResponseInfo("0","确认提交成功 ");
		}else {
			purchOrderComponent.updatePurchOrderStatus(orderId, 1);
		}
		
		return new ResponseInfo("0","提交保存成功 ");
	}

	/***
	 * 收货 
	 */
	@Override
	public ResponseInfo receive(JSONObject request) {

		Long orderId =  request.getLong("orderId");
		Integer type = request.getInteger("type");
		String qtysStr =request.getString("receive_qtys");
		List<JSONObject> listQty=JSONObject.parseArray(qtysStr,JSONObject.class);
		
		if(listQty!=null && !listQty.isEmpty()) {
			for(JSONObject jo : listQty) {
				Integer data_id = jo.getInteger("data_id");
				if(data_id == null || data_id==0) {
					continue ;
				}
				BigDecimal qty = jo.getBigDecimal("value");
				//
				PPurchOrderItems pPurchOrderItems= new PPurchOrderItems();
				pPurchOrderItems.setId((long)data_id);
				pPurchOrderItems.setReceiveQty(qty);
				pPurchOrderItemsDao.update(pPurchOrderItems);
			}
		}
		
		//确认提交 
		if(type==1) {
			purchOrderComponent.updatePurchOrderStatus(orderId, 6);
			//库存变化 
			List<PPurchOrderItems> listPPurchOrderItems=pPurchOrderItemsDao.listOrderId(orderId);
			stockComponent.purchStock(listPPurchOrderItems);
			
			return new ResponseInfo("0","确认提交成功 ");
		}else {
			purchOrderComponent.updatePurchOrderStatus(orderId, 5);
		}
		
		return new ResponseInfo("0","提交保存成功 ");
	}

}
